package com.itheima.controller.system.exception;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.controller.system.exception.results.Code;
import com.itheima.controller.system.exception.results.Results;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;
    @PostMapping
    public Results save(User user){
        boolean flag = userService.save(user);
        return new Results(flag ? Code.SAVE_OK:Code.SAVE_ERROR);
    }
    @PutMapping
    public Results update(@RequestBody User user){
        boolean flag = userService.update(user);
        return new Results(flag ? Code.SAVE_OK:Code.SAVE_ERROR);
    }

    @DeleteMapping("/{id}")
    public Results update(@PathVariable Integer id){
        boolean flag = userService.delete(id);
        return new Results(flag ? Code.SAVE_OK:Code.SAVE_ERROR);
    }
    @GetMapping("/{username}")
    public Results update(@PathVariable String username){
        User user = userService.getByUserName(username);
        return new Results(user !=null?Code.GET_OK:Code.GET_ERROR,null,user);
    }
    @PostMapping("/login")
    public Results getByUserNameAndPassword(User user){
        User user1 = userService.getByUserNameAndPassword(user.getUsername(),user.getPassword());
        System.out.println(user1);
        return new Results(user !=null?Code.GET_OK:Code.GET_ERROR,null,user);
    }
}
